/*
 * Copyright 2016-2036 the original author or authors.
 *
 * COMMERCIAL USE OF THIS SOFTWARE WITHOUT WARRANTY IS NOT ALLOWED.
 * Use is subject to license terms! You can distribute a copy of this software
 * to others for free. This software is a non-profit and open-source project.
 * Any contribution to this project will make it better.
 * All rights reserved! Owned by Stephen Liu.
 *
 */
package com.github.hingo

import com.github.hingo.entity.Major
import com.github.hingo.service.MajorService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.dao.DuplicateKeyException
import org.springframework.test.annotation.Rollback
import spock.lang.Title

/**
 * @author ste7en.liu@gmail.com
 * @since 2016/10/22
 */
@Title("Major Test")
class MajorSpecTest extends SpockSpec {

    @Autowired
    MajorService majorService

    def "Test find major"() {

        given: "Get ready for testing"
            def code1 = "4000001"
            def code2 = "400000x"

        when: "Find major"
            def major1 = majorService.findOne(code1)
            def major2 = majorService.findOne(code2)

        then: "major1 couldn't be null"
            major1 != null
            major1.name == "汉语言文学"
            major1.code == code1
            major1.type == 4
            major1.status == 0
        then: "major2 must be null"
            major2 == null

    }

    @Rollback
    def "Test add major"() {

        given: "Get ready for testing"
            def major = new Major()
            major.name = "外国语言文学"
            major.code = "4000002"
            major.type = 4
            major.status = 0
            major.gmtCreated = new Date()
            major.gmtModified = new Date()

        when: "Add a new major"
            majorService.add(major)
            def result = majorService.findOne(major.code)

        then: "Major should be successfully added into database"
            result != null
            result.name == major.name
            result.code == major.code
            result.type == major.type
            result.status == major.status

        when: "Add a major that has already existed"
            majorService.add(major)

        then: "Exception should occur"
            thrown(DuplicateKeyException.class)
    }
}
